package org.javahispano.javacup.model.engine;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import javax.swing.JOptionPane;
import org.javahispano.javacup.render.VisorBasico;
import org.javahispano.javacup.render.VisorOpenGl;

public class Applet extends java.applet.Applet {

    private File unzip(File file) throws Exception {
        ZipFile zf = new ZipFile(file.getAbsolutePath());
        Enumeration e = zf.entries();
        File f = null;
        ZipEntry ze = (ZipEntry) e.nextElement();
        f = new File(new File(ze.getName()).getName());
        FileOutputStream fout = new FileOutputStream(f);
        InputStream in = zf.getInputStream(ze);
        byte[] buffer = new byte[4096];
        int read = 0;
        while ((read = in.read(buffer)) > -1) {
            fout.write(buffer, 0, read);
        }
        in.close();
        fout.close();
        zf.close();
        return f;
    }

    private PartidoGuardado load(String url, int size) throws Exception {
        URL surl = new URL(url);
        System.out.println(surl.toString());
        InputStream is = null;
        Exception e = null;
        byte bytes[] = new byte[1];
        File tempFile = null;
        File unziped = null;
        try {
            tempFile = File.createTempFile("tmp", ".zip", new File("."));
            tempFile.deleteOnExit();
            FileOutputStream fos = new FileOutputStream(tempFile);
            is = surl.openStream();
            int read = 0;
            int readed = 0;
            jLabel1.setText(surl.toString());
            while ((read = is.read(bytes)) > -1) {
                readed = readed + read;
                fos.write(bytes, 0, read);
                jProgressBar1.setValue(100 * readed / size);
            }
            fos.close();
            is.close();
            unziped = unzip(tempFile);
            unziped.deleteOnExit();
            FileInputStream fis = new FileInputStream(unziped);
            ObjectInputStream ois = new ObjectInputStream(fis);
            PartidoGuardado p = (PartidoGuardado) ois.readObject();
            ois.close();
            fis.close();
            tempFile.delete();
            unziped.delete();
            p.iteracion = p.partido.get(0);
            return p;
        } catch (ClassNotFoundException ex) {
            e = ex;
        } catch (IOException ex) {
            e = ex;
        } finally {
            try {
                is.close();
            } catch (IOException ex) {
                e = ex;
            }
        }
        if (e != null) {
            JOptionPane.showMessageDialog(null, "Error al cargar partido", "ERROR", JOptionPane.ERROR_MESSAGE);
            throw e;
        }
        if (tempFile != null && tempFile.exists()) {
            tempFile.delete();
        }
        if (unziped != null && unziped.exists()) {
            unziped.delete();
        }
        return null;
    }

    @Override
    public void start() {
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    PartidoGuardado pg = load(url, 490680);
                    if (visor.equals("openGL")) {
                        VisorOpenGl.sonidos = sonido.equals("si");
                        VisorOpenGl visor = new VisorOpenGl(pg, 640, 480, false, null);
                    } else {
                        VisorBasico.sonidos = sonido.equals("si");
                        VisorBasico visor = new VisorBasico(pg, null);
                    }

                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "Error al cargar partido", "ERROR", JOptionPane.ERROR_MESSAGE);
                }
            }
        }).start();

    }
    String url = "file:/C:/Users/panxo/Desktop/1.jvc";
    String sesion = "872348947123";
    String visor = "openGL";
    String sonido = "no";

    @Override
    public void init() {
        //surl = getParameter("url");
        //sesion = getParameter("sesion");
        //visor = getParameter("visor");
        initComponents();
        setVisible(true);
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jProgressBar1 = new javax.swing.JProgressBar();
        jPanel3 = new javax.swing.JPanel();

        setLayout(new java.awt.BorderLayout());

        jPanel1.setLayout(new java.awt.CardLayout());

        jPanel2.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
        jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.PAGE_AXIS));
        jPanel2.add(jPanel4);

        jLabel1.setText("URL");
        jLabel1.setMaximumSize(new java.awt.Dimension(2048, 16));
        jLabel1.setMinimumSize(new java.awt.Dimension(2048, 16));
        jPanel2.add(jLabel1);
        jPanel2.add(jProgressBar1);
        jPanel2.add(jPanel3);

        jPanel1.add(jPanel2, "card2");

        add(jPanel1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JProgressBar jProgressBar1;
    // End of variables declaration//GEN-END:variables
}
